package com.cg.design.graphicdesignpattern.command.command;

import java.util.Stack;

/**
 * @program: LeetCode->MacroCommand
 * @description: 表示“由多条命令整合成的命令”的类，实现了Command接口
 * @author: cg
 * @create: 2022-04-05 19:20
 **/
public class MacroCommand implements Command {

    /**
     * 命令的集合
     */
    private final Stack<Command> commands = new Stack<>();

    @Override
    public void execute() {
        commands.forEach(Command::execute);
    }

    /**
     * 添加命令
     *
     * @param command
     */
    public void append(Command command) {
        if (command != this) {
            commands.push(command);
        }
    }

    /**
     * 删除最后一条命令
     */
    public void undo() {
        if (!commands.empty()) {
            commands.pop();
        }
    }

    /**
     * 删除所有命令
     */
    public void clear() {
        commands.clear();
    }

}
